"""SQLAlchemy Metadata and Session object"""
from sqlalchemy import Table, Column, Integer, String, Unicode, MetaData, ForeignKey
from sqlalchemy.orm import scoped_session, sessionmaker, mapper

__all__ = ['Session', 'engine', 'metadata']

# SQLAlchemy database engine. Updated by model.init_model()
engine = None

# SQLAlchemy session manager. Updated by model.init_model()
Session = scoped_session(sessionmaker())

# Global metadata. If you have multiple databases with overlapping table
# names, you'll need a metadata for each database
metadata = MetaData()

# [Table: Users]
# CREATE TABLE `users` (
#  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
#  `username` varchar(50) DEFAULT NULL,
#  `password` longtext,
#  `realname` varchar(250) DEFAULT NULL,
#  `email` varchar(250) DEFAULT NULL,
#  PRIMARY KEY (`id`)
# ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8

table_users = Table('users', metadata,
    Column('id', Integer, primary_key = True),
    Column('username', String),
    Column('password', Unicode),
    Column('realname', Unicode),
    Column('email', String)
)

class TUser(object):
    id = 0
    username = None
    password = None
    realname = None
    email = None
    
    def __init__(self, id, username, password, realname, email):
        """
        A data structure representing users
        """
        self.id = id
        self.username = username
        self.password = password
        self.realname = realname
        self.email = email
    
    def __repr__(self):
        return "<TUser(%d, '%s', '%s', '%s', '%s')>"\
            % (
                self.id,
                self.username,
                self.password,
                self.realname,
                self.email
            )
    
    def dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'password': self.password,
            'realname': self.realname,
            'email': self.email
        }
    
    def list(self):
        return [
            self.id,
            self.username,
            self.password,
            self.realname,
            self.email
        ]
        
mapper(TUser, table_users)